package com.platform.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.entity.ApiCommentVo;
import com.platform.entity.CommentEntity;
import com.platform.entity.UserEntity;
import com.platform.service.CommentService;
import com.platform.service.OrderService;
import com.platform.service.UserService;
import com.platform.util.UtilServiceName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.entity.OrderEntity;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-03 15:04:03
 */
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("apiorder:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<OrderEntity> apiOrderList = orderService.queryList(query);
        for (OrderEntity info : apiOrderList) {
            UserEntity zbUser = userService.queryObject(info.getZbUserId());
            info.setZbNickName(zbUser != null ? zbUser.getNickname() : "");
            UserEntity cmUser = userService.queryObject(info.getCmUserId());
            info.setCmNickName(cmUser != null ? cmUser.getNickname() : "");
        }

        int total = orderService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiOrderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/commentList")
    @RequiresPermissions("apiorder:list")
    @ResponseBody
    public R commentList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CommentEntity> commentVos = commentService.queryList(query);

        List<Map<String, Object>> list = new ArrayList<>();
        for (CommentEntity info : commentVos) {
            Map<String, Object> map = new HashMap<>();
            map.put("time", info.getCreateDate());
            map.put("content", info.getContent());
            UserEntity userEntity = userService.queryObject(info.getCommentUserId());
            if (userEntity != null) {
                map.put("zbName", userEntity.getNickname());
            }

            map.put("mouldName", UtilServiceName.serviceNmae(info.getMouldName()));
            list.add(map);
        }
        int total = commentService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("apiorder:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        OrderEntity apiOrder = orderService.queryObject(id);

        return R.ok().put("apiOrder", apiOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("apiorder:save")
    @ResponseBody
    public R save(@RequestBody OrderEntity apiOrder) {
        orderService.save(apiOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("apiorder:update")
    @ResponseBody
    public R update(@RequestBody OrderEntity apiOrder) {
        orderService.update(apiOrder);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("apiorder:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        orderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<OrderEntity> list = orderService.queryList(params);

        return R.ok().put("list", list);
    }
}
